const util = require('../../../utils/util')
const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter
const oss_domain = getApp().globalData.oss_domain
const status_list = getApp().globalData.status_list
const status_bgColors = getApp().globalData.status_bgColors
const license_list = getApp().globalData.license_list
const pilot_rank_list = getApp().globalData.pilot_rank_list
const uav_type_list = getApp().globalData.uav_type_list
const uav_level_list = getApp().globalData.uav_level_list
const user_id = wx.getStorageSync('user_id')
const right_list = ['is_admin',1,2,3,4]
const today = util.formatDate(new Date())
const windowWidth = wx.getSystemInfoSync().windowWidth
// pages/students/passing_rate/passing_rate.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uav_level_list,
    uav_type_list,
    windowWidth,
    today,
    right_list,
    pilot_rank_list,
    license_list,
    user_id,
    status_bgColors,
    status_list,
    this_status_list:[
      {id:'', name:'- 全部 -'},{id:3, name:'在训'},{id:9, name:'初考'},{id:10, name:'补考'}
    ],
    date_set:'',
    stu_name:'',
    ins_name:'',
    ins_id:'',
    ins_index:0,
    icon_url:oss_domain + 'static/picture/app_icon/',
    page_no:0,
    page_size:20,
    lis:[],
    stu:{},
    select_stu_index:0,
    loading:true,
  },

  select_stu(e){
    getApp().get_vibration()
    let select_stu_index = e.currentTarget.id
    this.setData({
      select_stu_index,
      stu:this.data.lis[select_stu_index]
    })
  },

  view_stu(e){
    getApp().get_vibration()
    let jg_stu_id = this.data.stu.jg_stu_id
    wx.navigateTo({
      url: '/pages/students/stu_register/stuRegister_info/stuRegister_info?jg_stu_id=' + jg_stu_id
    })
  },

  input_stu_name(e){
    this.setData({
      stu_name:e.detail.value
    })
  },


  show_today(){
    getApp().get_vibration()
    if(this.data.date_set==today){
      this.setData({
        date_set:''
      })
    }else{
      this.setData({
        date_set:today
      })
    }
    this.data.page_no = 0
    this.training_monitor_load_stus(this.data.jg_id, this.data.page_no, this.data.page_size,this.data.ins_id, this.data.stu_name,this.data.date_set)
  },

  go_search(){
    // console.log('go_search')
    this.setData({
      page_no:0,
      ins_id:'',
      select_stu_index:0
    })
    this.training_monitor_load_stus(this.data.jg_id, this.data.page_no, this.data.page_size, this.data.ins_id, this.data.stu_name,this.data.date_set)
  },

  cancel_search(){
    this.setData({
      page_no:0,
      ins_id:'',
      stu_name:''
    })
    this.training_monitor_load_stus(this.data.jg_id, this.data.page_no, this.data.page_size, this.data.ins_id, this.data.stu_name,this.data.date_set)
  },

  view_photo(e){
    wx.previewImage({
      urls: [this.data.stu.photo],
      current: this.data.stu.photo,
    })
  },
  
  test_stage(e){
    getApp().get_vibration()
    let index = e.currentTarget.dataset.index
    let stage_id = this.data.stu.test_stages[index].id
    let jg_stu_id = this.data.stu.jg_stu_id
    let name = this.data.stu.name
    if(stage_id==1){
      var url_data = {jg_stu_id,stu_name:name,chap_id_list:[1,2,3,4,5,6,7,8,9]}
      var url = '/pages/students/stu_chapters/stu_chapters?url_data=' + encodeURIComponent(JSON.stringify(url_data))
    }else if(stage_id==2){
      var url_data = {jg_stu_id,stu_name:name,chap_id_list:[17]}
      var url = '/pages/students/stu_chapters/stu_chapters?url_data=' + encodeURIComponent(JSON.stringify(url_data))
    }else{
      var url_data = {
        jg_id : this.data.jg_id,
        jg_stu_id : jg_stu_id,
        stage_id : stage_id,
      }
      var url = '/pages/students/test_stu/test_stu?url_data=' + encodeURIComponent(JSON.stringify(url_data))
    }
    wx.navigateTo({url})
    this.can_onShow = true
  },



  training_monitor_load_stus(jg_id, page_no, page_size, ins_id, stu_name, date_set){
    wx.showLoading()
    this.bindscrolltolower_locked = 1
    wx.request({
      url: domainName + 'training_monitor_load_stus',
      data: {jg_id,page_no,page_size,ins_id,stu_name,user_id,date_set},...parameter,
      success: res => {
      this.bindscrolltolower_locked = 0
        wx.hideLoading()
        console.log('res',res.data)
        let lis = res.data.lis
        this.setData({
          loading:false,
          page_no:this.data.page_no + 1,
          no_more:lis.length==0?true:false,
          lis:page_no==0?lis:this.data.lis.concat(lis),
          stu_count:res.data.stu_count,
        })

        if(page_no==0){
          this.setData({
            stu:lis[this.data.select_stu_index] || ''
          })
        }

        wx.setNavigationBarTitle({
          title: '（'+this.data.lis.length+'）学员：培训进度',
          success: (res) => {},
          fail: (res) => {},
          complete: (res) => {},
        })
      },
    })
  },


  // 选择教员
  select_ins(e){
    getApp().get_vibration()
    let index = e.currentTarget.id
    if(index==this.data.ins_index){
      return
    }
    let ins_id = this.data.ins_list[index].ins_id
    this.setData({
      ins_index:index,
      ins_id,
      page_no:0,
      select_stu_index: 0

    })
    this.training_monitor_load_stus(this.data.jg_id, this.data.page_no, this.data.page_size,this.data.ins_id,this.data.stu_name,this.data.date_set)
  },



  page_init(user_id, jg_id){
    wx.request({
      url: domainName + 'training_monitor_init',
      data: {user_id, jg_id},...parameter,
      success: res => {
        console.log('page_init',res.data)
        this.setData({
          ins_list:res.data.ins_list,
          right_limit:res.data.right_limit,
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log('options',options)
    this.data.jg_id = options.jg_id
    this.training_monitor_load_stus(this.data.jg_id, this.data.page_no, this.data.page_size,this.data.ins_id,this.data.stu_name,this.data.date_set)
    this.page_init(user_id, this.data.jg_id)
  },

  load_more(){
    getApp().get_vibration()
    if(!this.data.no_more && !this.bindscrolltolower_locked){
      this.training_monitor_load_stus(this.data.jg_id, this.data.page_no, this.data.page_size,this.data.ins_id,this.data.stu_name,this.data.date_set)
    }
  },

  refresh(){
    getApp().get_vibration()
    this.data.page_no = 0
    this.training_monitor_load_stus(this.data.jg_id, this.data.page_no, this.data.lis.length,this.data.ins_id,this.data.stu_name, this.data.date_set)
  },

  // onShow(){
  //   if(this.can_onShow){
  //     this.data.page_no = 0
  //     this.training_monitor_load_stus(this.data.jg_id, this.data.page_no, this.data.lis.length,this.data.ins_id,this.data.stu_name, this.data.date_set)
  //     this.can_onShow = false
  //   }
  // },

  onReady(){
    this.setData({windowHeight:wx.getSystemInfoSync().windowHeight})
  }
})